import { RJSFSchema, UiSchema } from '@rjsf/utils';
import { CollapseTemplate } from 'nextcae-base';
import { ElementTypes, MethodType } from './type';
import { i18n } from '@/common/utils';
import { SelectionPickField2 } from '@/components/FieldTemplate/SelectionPickField2';

export const schema: RJSFSchema = {
  type: 'object',
  properties: {
    Selection: {
      type: 'object',
      title: i18n.t('Selection'),
      properties: {
        source: {
          title: i18n.t('Source'),
          type: 'array',
          ...{
            'x-options': ['face'],
          },
        },
      },
    },
    MeshSettings: {
      type: 'object',
      title: i18n.t('Mesh Settings'),
      properties: {
        elementType: {
          title: i18n.t('Element Type'),
          type: 'string',
          default: ElementTypes.Tri,
          oneOf: [
            {
              const: ElementTypes.Tri,
              title: i18n.t('Tri'),
            },
            {
              const: ElementTypes.Quad,
              title: i18n.t('Quad'),
            },
            // 内核暂时不支持，注释掉
            // {
            //   const: ElementTypes.StructuredQuad,
            //   title: i18n.t('Structured Quad'),
            // },
            {
              const: ElementTypes.Mixed,
              title: i18n.t('Mixed'),
            },
            {
              const: ElementTypes.RTri,
              title: i18n.t('RTri'),
            },
          ],
        },
        // order: {
        //   title: 'Order',
        //   type: 'number',
        //   default: OrderTypes.OneST,
        // },
      },
      allOf: [
        {
          if: {
            properties: {
              elementType: { enum: [ElementTypes.Quad, ElementTypes.Mixed] },
            },
          },
          then: {
            properties: {
              method: {
                title: i18n.t('Method'),
                type: 'string',
                oneOf: [
                  {
                    const: '',
                    title: '合并法',
                  },
                  {
                    const: MethodType.Transfinite,
                    title: i18n.t(MethodType.Transfinite),
                  },
                ],
              },
            },
          },
        },
        {
          if: {
            properties: {
              elementType: { const: ElementTypes.Tri },
            },
          },
          then: {
            properties: {
              method: {
                title: i18n.t('Method'),
                type: 'string',
                oneOf: [
                  {
                    const: MethodType.AFTDelaunay,
                    title: i18n.t(MethodType.AFTDelaunay),
                  },
                  {
                    const: MethodType.Delaunay,
                    title: i18n.t(MethodType.Delaunay),
                  },
                ],
              },
            },
          },
        },
      ],
    },
    MeshSize: {
      type: 'object',
      title: i18n.t('Mesh Size'),
      properties: {
        maxLength: {
          title: i18n.t('Max Length'),
          type: 'number',
          default: 20,
        },
        minLength: {
          title: i18n.t('Min Length'),
          type: 'number',
          default: 0,
        },
        growthRate: {
          title: i18n.t('Growth Rate'),
          type: 'number',
          default: 1,
        },
      },
    },
    Curvature: {
      type: 'object',
      title: i18n.t('Curvature'),
      properties: {
        curvatureFactor: {
          title: i18n.t('Curvature Factor'),
          type: 'number',
          default: 0.2,
        },
      },
    },
  },
};

export const uiSchema: UiSchema = {
  Selection: {
    'ui:FieldTemplate': CollapseTemplate,
    'ui:layout': [1, 2],
    'ui:options': {
      label: false, // 不显示 label
    },
    source: {
      'ui:field': SelectionPickField2,
      'ui:focused': true,
    },
  },
  MeshSettings: {
    'ui:FieldTemplate': CollapseTemplate,
    'ui:layout': [1, 2],
    'ui:options': {
      label: false, // 不显示 label
    },
    method: {
      'ui:field': 'selectionField',
    },
    elementType: {
      'ui:field': 'selectionField',
    },
    // order: {
    //   'ui:field': 'selectionField',
    //   'ui:options': {
    //     options: [
    //       {
    //         const: OrderTypes.OneST,
    //         title: '1st',
    //       },
    //       {
    //         const: OrderTypes.TowND,
    //         title: '2nd',
    //       },
    //     ],
    //   },
    // },
  },
  MeshSize: {
    'ui:FieldTemplate': CollapseTemplate,
    'ui:layout': [1, 2],
    'ui:options': {
      label: false, // 不显示 label
    },
    maxLength: {
      'ui:field': 'inputField',
      'ui:type': 'number',
    },
    minLength: {
      'ui:field': 'inputField',
      'ui:type': 'number',
    },
    growthRate: {
      'ui:field': 'inputField',
      'ui:type': 'number',
    },
  },
  Curvature: {
    'ui:FieldTemplate': CollapseTemplate,
    'ui:layout': [1, 2],
    'ui:options': {
      label: false, // 不显示 label
    },
    curvatureFactor: {
      'ui:field': 'inputField',
      'ui:type': 'number',
    },
  },
};
